


import bpy
import bmesh

import 全局控制.Node.节点和插座基类.bl_节点基类 as 基类
import 节点.Core.bl_Mesh as 网格操作
import 节点.Core.bl_节点构建模板 as 构建模板
import 物体.物体管理.物体数据 as 物体数据

from itertools import chain
from itertools import combinations

import engine.initLIB.load_dll as engineLib
import ctypes





engineLib.flexCAPI.f_设置物体网格节点.restype = ctypes.c_bool







E_类型枚举 = [
	("TYPE_父级", "parect", "", 0),
	
]






def f_update设置物体网格_平滑(self, context):
	data = (list("平滑"), int(self.m_平滑))
	engineLib.flexCAPI.f_node_传入BL数据(ctypes.c_wchar_p(self.id_data.name), ctypes.c_wchar_p(self.name), ctypes.py_object(data));

def f_update设置物体网格_坐标系转换(self, context):
	data = (list("坐标系转换"), int(self.m_坐标系转换))
	engineLib.flexCAPI.f_node_传入BL数据(ctypes.c_wchar_p(self.id_data.name), ctypes.c_wchar_p(self.name), ctypes.py_object(data));
	if context :
		self.id_data.f_节点数据更新_更新树([self])

def f_update设置物体网格(self, context):
	if self.m_对象 :
		data = list(self.m_对象.name)
	else:
		data = []
	data = (list('对象'), data)
	engineLib.flexCAPI.f_node_传入BL数据(ctypes.c_wchar_p(self.id_data.name), ctypes.c_wchar_p(self.name), ctypes.py_object(data));

class C_Node_设置网格物体(基类.C_Node_基类):
	bl_dname = 'bl_node_设置物体网格'
	bl_label = '设置网格物体'
	
	m_对象 = bpy.props.PointerProperty(type=bpy.types.Object, update=f_update设置物体网格)
	m_平滑 = bpy.props.BoolProperty(update=f_update设置物体网格_平滑)
	m_坐标系转换 = bpy.props.BoolProperty(update=f_update设置物体网格_坐标系转换)
	#m_文本类型 = bpy.props.EnumProperty(items=E_文件类型枚举, name="类型", default="pyx", update=f_update_载入网格参数)
	def init(self, context):
		super(C_Node_设置网格物体, self).f_init(context)
		构建模板.f_创建网格插座(self, False)
		self.inputs.new('C_Socket_网格', name='网格')
		#self.outputs.new('C_Socket_网格', name='网格')
		self.f_toCPP(self.id_data)
		
	def draw_buttons(self, context, layout):
		layout.prop(self, "m_对象")
		layout.prop(self, "m_平滑")
		layout.prop(self, "m_坐标系转换")
		
	def f_toCPP(self, tree):
		是否更新 = engineLib.flexCAPI.f_设置物体网格节点(ctypes.c_wchar_p(tree.name), ctypes.c_wchar_p(self.name), self.f_getPyObject(tree));
		#f_update_载入网格参数(self, None)
	def f_update(self):
		if self.m_对象 :
			#meshs = self.inputs['网格'].f_getData()
			meshs = engineLib.flexCAPI.f_取节点数据(ctypes.c_wchar_p(self.id_data.name), ctypes.c_wchar_p(self.name));
			print('meshs ==== ', meshs)
			bm = bmesh.new()
			bm.to_mesh(self.m_对象.data)
			bm.free()
			#print(网格数据, '=网格数据')
			if meshs :
				网格操作.f_构建bl网格(self.m_对象.data, meshs)
				#print('-----------------end---------------------')
				if self.m_坐标系转换:
					物体数据.f_右手坐标系转左手坐标系(self.m_对象.data)

				if self.m_平滑 :
					self.m_对象.select_set(True)
					bpy.ops.object.shade_smooth()
				return

			顶点 = self.inputs['顶点'].f_getData()
			print('顶点 = ', 顶点, len(顶点))
			if 顶点 :
				网格操作.f_填充bl网格顶点(self.m_对象.data, 顶点)
				
				if self.m_坐标系转换:
					物体数据.f_右手坐标系转左手坐标系(self.m_对象.data)
			


class C_NODE_MENU_设置网格物体(bpy.types.Operator):
	bl_idname = 'node.global_node_set_object_mesh'
	bl_label = '设置网格物体'
	bl_options = {'REGISTER', 'UNDO'}
	def execute(operator, context):
		bpy.ops.node.add_node(type='C_Node_设置网格物体', use_transform=True)
		return {'FINISHED'}

	














class C_Node_网格成员展开(基类.C_Node_基类):
	bl_dname = 'bl_node_网格成员展开'
	bl_label = '网格成员展开'
	
	def init(self, context):
		super(C_Node_网格成员展开, self).f_init(context)
		self.inputs.new('C_Socket_网格', name='网格')

		构建模板.f_创建网格插座(self, True)
		self.outputs.new('C_Socket_网格', name='网格')
		self.f_toCPP(self.id_data)

	def f_toCPP(self):
		engineLib.flexCAPI.f_创建网格成员展开节点(ctypes.c_wchar_p(self.id_data.name), ctypes.c_wchar_p(self.name), self.f_getPyObject());
		
		
class C_NODE_MENU_网格成员展开(bpy.types.Operator):
	bl_idname = 'node.mesh_member_expansion'
	bl_label = '网格成员展开'
	bl_options = {'REGISTER', 'UNDO'}
	def execute(operator, context):
		bpy.ops.node.add_node(type='C_Node_网格成员展开', use_transform=True)
		return {'FINISHED'}











class C_Node_网格合并成员(基类.C_Node_基类):
	bl_dname = 'bl_node_网格合并成员'
	bl_label = '网格合并成员'
	
	def init(self, context):
		super(C_Node_网格合并成员, self).f_init(context)

		构建模板.f_创建网格插座(self, False)
		self.inputs.new('C_Socket_网格', name='网格')
		
		self.outputs.new('C_Socket_网格', name='网格')
		self.f_toCPP(self.id_data)

	def f_toCPP(self):
		engineLib.flexCAPI.f_创建网格合并成员节点(ctypes.c_wchar_p(self.id_data.name), ctypes.c_wchar_p(self.name), self.f_getPyObject());
		
		
class C_NODE_MENU_网格合并成员(bpy.types.Operator):
	bl_idname = 'node.mesh_member_merger'
	bl_label = '网格合并成员'
	bl_options = {'REGISTER', 'UNDO'}
	def execute(operator, context):
		bpy.ops.node.add_node(type='C_Node_网格合并成员', use_transform=True)
		return {'FINISHED'}






classes = (
	#C_Node_设置物体网格,
	#C_NODE_MENU_设置物体网格,

	C_Node_设置网格物体,
	C_NODE_MENU_设置网格物体,

	C_Node_网格成员展开,
	C_NODE_MENU_网格成员展开,

	C_Node_网格合并成员,
	C_NODE_MENU_网格合并成员,


)

def register():
	for c in classes:
		bpy.utils.register_class(c)
		
def unregister():
	for c in classes:
		bpy.utils.unregister_class(c)



